<template>
  <div>
    <el-form :inline="true" :model="dataForm">
      <p class="part-title" style="margin-top: 5px;">所属实验</p>
      <el-form-item label="请选择实验:">
        <el-select v-model="dataForm.experimentName" placeholder="请选择"style="width: 240px;"clearable>
            <el-option
              v-for="item in experimentName"
              :key="item.uuid"
              :label="item.experimentName"
              :value="item.experimentName">
            </el-option>
          </el-select>
      </el-form-item>
      <p class="part-title" style="margin-top: 5px;">聊天室开设时间</p>
      <el-form-item label="请选择开设时间:">
      <el-date-picker v-model="time" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" prop="time"
       range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right">
      </el-date-picker>
      </el-form-item>
      <p class="part-title" style="margin-top: 5px;">课题任务发布</p>
      <el-form-item label="请发布课题任务:" prop="task" >
        <el-input type="textarea" :rows="5" v-model="dataForm.task" style="width: 500px;"></el-input>
      </el-form-item>
      <p class="part-title" style="margin-top: 5px;">课题资料共享</p>
      <el-form-item label="请上传实验资料:" prop="file" style="width: 400px;">
        <el-upload
          class="upload-demo"
          :on-success="handleAvatarSuccess"
          :action="saveImg()"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :file-list="fileList">
          <el-button size="small" type="primary">点击上传资料</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <div style="margin-top: 30px;"></div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSave()" >保存</el-button>
      <el-button  @click="dataCancel('dataForm')" >重置</el-button>
    </span>
  </div>
</template>

<script>
  /* eslint-disable */
  export default {
    data () {
      return {
        dataForm:{
          experimentName:'',
          task:'',
          file:''
        },
        fileList: [],
        time:'',
        experimentName: [],
        files:[]
      }
    },
    activated () {
      this.getExperimentList()
    },
    computed: {
      account: {
        get () { return this.$store.state.user.account },
        set (val) { this.$store.commit('user/updateAccount', val) }
      }
    },
    methods: {
      // 获取所有的实验
      getExperimentList () {
        this.$http({
          url: this.$http.adornUrl('/course/getExperimentName'),
            method: 'post',
            data: this.$http.adornData({
              teacherAccount: this.account
            })
        }).then(({data}) => {
          if(data.success){
            this.experimentName = data.data
          } else {
            this.$message.error(data.message)
          }
        })
      },
      handleAvatarSuccess(response) {
        if (response.success) {
          this.files.push(response.data)
        }else{
          this.$message.error(response.message)
        }
      },
      // 保存图片
      saveImg(){
        return this.$http.adornUrl('/file/image');
      },
      handleRemove(file, fileList) {
        fileList.push(file);
      },
      handlePreview(file) {
        fileList.remove(file)
      },
      dataCancel(dataForm) {
        this.dataForm = {
          experimentName:'',
          task:'',
          file:''
        },
        this.fileList = [],
        this.time='',
        this.files=[]
      },
      // 表单提交
      dataFormSave () {
        this.dataForm.file = this.files[0]
        this.startTime = this.time[0]
        this.endTime = this.time[1]
        this.$http({
          url: this.$http.adornUrl('/task/addTask'),
          method: 'post',
          data: this.$http.adornData({
            'experimentName': this.dataForm.experimentName,
            'account': this.account,
            'task': this.dataForm.task,
            'file': this.dataForm.file,
            'startTime': this.startTime,
            'endTime': this.endTime
          })
        }).then(({data}) => {
          if (data.success) {
            this.$message({
              message: '任务发布成功!',
              type: 'success'
           });
           this.files = [],
           this.dataCancel(this.dataForm),
           this.$router.push("/task-taskList")
          } else {
            this.$message.error(data.message)
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .part-title {
      margin: 20px 0;
      color: #000;
      padding-left: 1em;
      font-size: 16px;
      position: relative;
      display: inline-block;
      width: 100%;
  }
  .part-title:before{
    content: "";
    position: absolute;
    left: 0;
    top: 4px;
    width: 3px;
    height: 1em;
    background-color: #4976e8;
  }
 .el-button--primary {
      color: #fff;
      background-color: #273471 !important;
      border-color: #273471 !important;
  }
</style>
